
//
//  HLSearchTitleView.swift
//  MyBlog
//
//  Created by 李芙蓉 on 16/3/4.
//  Copyright © 2016年 hanlei. All rights reserved.
//

import UIKit

class HLSearchTitleView: UIView {

    //懒加载textField
    private lazy var textInput : UITextField = {
        
        let t = UITextField()
        
        t.backgroundColor = myBlueColor
        
        t.layer.cornerRadius = 5

        t.layer.masksToBounds = true
        
        t.layer.borderColor = UIColor.blueColor().CGColor
        
        t.layer.borderWidth = 1
        
        t.addTarget(self, action: "textInputStartEditing", forControlEvents: UIControlEvents.EditingDidBegin)
        
        t.placeholder = "请输入搜索内容"
 
        return t
    }()
    
    //懒加载取消按钮
    private lazy var cancelButton : UIButton = {
       
        let c = UIButton()
        
        c.hidden = true
        
        c.addTarget(self, action: "cancelButtonDidClick:", forControlEvents: .TouchUpInside)
        
        c.setTitle("取消", forState: .Normal)
        
        c.setTitleColor(myTextColor, forState: .Normal)
        
        c.setTitleColor(myOrangeColor, forState: .Highlighted)
        
        return c
    }()
    
    //懒加载textInput的leftView
    private lazy var leftImageView : UIImageView = {
        
        let l = UIImageView(image: UIImage(named: "searchbar_textfield_search_icon"))
        
        l.contentMode = .Center
        
        return l
    }()
    
    //记录textInput的右侧约束
    var rightConstraint : NSLayoutConstraint?
    
    //重写构造函数
    override init(frame: CGRect) {
        
        super.init(frame: frame)
        
        addSubview(cancelButton)
        
        addSubview(textInput)
        
        textInput.leftView = leftImageView
        
        textInput.leftViewMode = UITextFieldViewMode.Always
        
        textInput.leftView?.frame = CGRect(x: 0, y: 0, width: bounds.height, height: bounds.height)
        
        //设置textInput的约束
        textInput.translatesAutoresizingMaskIntoConstraints = false
        
        self.addConstraint(NSLayoutConstraint(item:textInput, attribute: .Top, relatedBy: .Equal, toItem: self, attribute: .Top, multiplier: 1, constant: 0))
        
        self.addConstraint(NSLayoutConstraint(item:textInput, attribute: .Left, relatedBy: .Equal, toItem: self, attribute: .Left, multiplier: 1, constant: 0))
        
        self.addConstraint(NSLayoutConstraint(item:textInput, attribute: .Bottom, relatedBy: .Equal, toItem: self, attribute: .Bottom, multiplier: 1, constant: 0))
        
        rightConstraint = NSLayoutConstraint(item:textInput, attribute: .Right, relatedBy: .Equal, toItem: self, attribute: .Right, multiplier: 1, constant: 0)
        
        self.addConstraint(rightConstraint!)
        
        //设置cancelButton的约束
        cancelButton.translatesAutoresizingMaskIntoConstraints = false
        
        self.addConstraint(NSLayoutConstraint(item: cancelButton, attribute: .Top, relatedBy: .Equal, toItem: self, attribute: .Top, multiplier: 1, constant: 0))
        
        self.addConstraint(NSLayoutConstraint(item: cancelButton, attribute: .Right, relatedBy: .Equal, toItem: self, attribute: .Right, multiplier: 1, constant: 0))
        
        self.addConstraint(NSLayoutConstraint(item: cancelButton, attribute: .Bottom, relatedBy: .Equal, toItem: self, attribute: .Bottom, multiplier: 1, constant: 0))
        
        self.addConstraint(NSLayoutConstraint(item: cancelButton, attribute: .Width, relatedBy: .Equal, toItem: .None, attribute: .NotAnAttribute, multiplier: 1, constant: 44))

    }
    
    //布局子控件
    override func layoutSubviews() {
        
        super.layoutSubviews()
        
        //不能在此处添加约束！！
        //因为在实例化的时候会进入这个函数，在之后每次重绘的时候也会进入这个方法，相当于添加了多次约束！
        
    }
    
    //取消按钮的点击事件
    func cancelButtonDidClick(sender:UIButton) {
        
        rightConstraint?.constant = 0
        
        textInput.resignFirstResponder()
        
        UIView.animateWithDuration(0.5) { () -> Void in
            
            self.cancelButton.hidden = true
            
            self.textInput.layoutIfNeeded()
        }
    }
    
    //textInput开始编辑事件
    func textInputStartEditing () {
        
        rightConstraint?.constant = -44
        
        UIView.animateWithDuration(0.5) { () -> Void in
            
            self.cancelButton.hidden = false
            
            self.textInput.layoutIfNeeded()
        }
    }

    required init?(coder aDecoder: NSCoder) {
        fatalError("init(coder:) has not been implemented")
    }
}

